// 
//  toolbar.js
//  antbar
//  
//  Created by Zak on 2008-06-11.
//  Copyright 2008 Ant.com. All rights reserved.
// 

top.AntBar =
{
    searchType: "web",

    /**
     * Entry point of our toolbar, called before the loading of the homepage,
     * but after that the toolbar has been rendered
     */
    init: function ()
    {
        AntDownloadManager.init();
        AntFlvUi.init(document);
        AntGrabber.init();
        AntPrefs.init();
        AntRank.init();
        AntWatchUrl.init();
        if (AntPrefs.isExpertMode())
            $("antToolBarMovieListButton").setAttribute("hidden", false);
    },
    
    /**
     * Handle search using terms typed in the antToolBarSearchBox
     * @param type          The type of search (web,image,video,news,blog)
     */
    doSearch: function ()
    {
        var searchTermsBox = $("antToolBarSearchBox");
        var searchTerms = searchTermsBox.value.trim();

        if (searchTerms.length == 0 || !cleanedSearchBox)
            AntLib.openURL('http://www.ant.com/');
        else
            AntLib.openURL('http://www.ant.com/'+this.searchType+'/'+AntLib.uriEncode(searchTerms));
    },

    /**
     * Called when clicking on the "Support" button
     */
    onSupportButtonClick: function (event)
    {
        AntLib.openURL('http://contact.ant.com/');
        $('antToolBarCombinedButton').style.borderSpacing = 0;
    },

    /**
     * Called when clicking on the "Download" button
     */
    onDownloadButtonClick: function (event)
    {
		var doc = gBrowser.selectedBrowser.contentDocument;

		if (AntPrefs.isExpertMode() && !AntPrefs.isSiteEnabled(doc.location.href))
		{
			alert(AntLang.getString("AntBar.DownloadButtonExpertMode"));
			return ;
		}
		if (doc.__antflv__)
			AntFlvList.download(doc.__antflv__);
    },

    /**
     * Called when clicking on the "open directory" button
     */
    onOpenDirButtonClick: function (event)
    {
	flvDir = CCSV("@mozilla.org/file/directory_service;1", "nsIProperties").get("ProfD", CI("nsILocalFile"));
	flvDir.append("antbar");
        try
        {
	        flvDir.reveal();
        }
        catch (e)
        {
		try
		{
			AntLib.openExternal(flvDir);
		}
		catch (e2)
		{
               	 	AntLib.toLog("reveal error : " + e);
               	 	AntLib.toLog("openExternal error : " + e2);
                	alert("This functionnality is not available on your operating system");
		}
        }
    },
    /**
      * Called when click on the rank button
      */
    onRankButtonClick: function ()
    {
      AntRank.goProfil();  
    },

    /**
     * Called when clicking on the "Download" button
     */
    onMovieListButtonClick: function (event)
    {
		var doc = gBrowser.selectedBrowser.contentDocument;

		if (doc.location.href != "chrome://antbar/content/player/player.xul")
			AntFlvUi.togglePanel();
    },

    /**
     * Called when clicking on the "Player" button
     */
    onPlayerButtonClick: function (event)
    {
        gBrowser.selectedTab = gBrowser.addTab("chrome://antbar/content/player/player.xul");
        AntFlvUi.togglePanel(false);
    },

    /**
     * Clear the searchbox the first time user enter it
	 * @param isFocus true if the callback is onFocus otherwise false
     */
    onSearchBoxOnFocus: function (isFocus)
    {
        if (!cleanedSearchBox && isFocus)
        {
            $('antToolBarSearchBox').value = '';
            $('antToolBarSearchBox').style.color = '#000000';
            cleanedSearchBox = true;
        }
		if (!isFocus && !$('antToolBarSearchBox').value)
		{
            $('antToolBarSearchBox').value = AntLang.getString("AntBar.SearchWithAnt"); 
            $('antToolBarSearchBox').style.color = '#777777';
            cleanedSearchBox = false;
		}
    },

    /**
     * Handle keystrokes entered in the searchBox
     * @param event         The event that triggered the call to this function
     */
    onSearchBoxKeyPress: function (event)
    {
        if (event.keyCode == event.DOM_VK_RETURN && $('antToolBarSearchBox').value != '')
        	this.doSearch();
    },
    
    /**
     * Add the current site to the watch list, when in expert mode
     */
    onAddToWatchListButtonClick: function ()
    {
        AntPrefs.addSite(content.document.location.href);
        AntFlvUi.setContent("list");
    },
    
    /**
     * Disable the current website when in expert mode
     */
    onWatchDisableButtonClick: function ()
    {
        AntPrefs.removeSite(content.document.location.href);
        AntFlvUi.setContent("settings");
    }
}
